Git branches

Published

Tue, 15 of October, 2024

Modified

Tue, 15 of October, 2024

Caution

Web page construction in progress…

Branches

Create & checkout a branch

“checkout” means to change the branch you are currently working on (or switch to)

# 1/2 create b
git branch page_col #create a new branch named "page_col"

# 2/2 then switch to b
git checkout page_col

# or 1+2/2 CREATE + SWITCH BRANCHES
git checkout -b page_col  

Now I see
Git chekout branch

Switch to other branch

You can also use git switch other_branch which is more specific

git switch page_col
cat .git/HEAD # (confirms me I moved)

Rename a (local) branch

It’s the -m parameter !

  • you cannot rename a remte branch –> you delete it and re-upload it
# In currently checkedout 
git branch -m better_name

# in different branch (non HEAD)
git switch master
git branch test_branch # fake one 
git branch -a  # it's there
git branch -m test_branch test_branch2
git branch -a  # yep!

Push upstream a local branch

  1. Create local branch
  2. Switch to local branch
  3. git push –-set-upstream command (the 1st time you push)
  4. Thereafter git push (all subsequent git push commands automatically move local branch changes up to the remote branch.)
# 1 Create local branch 
# 2 switch to local branch 
git switch page_col
# 3.a git push –set-upstream command (1st time)
git push  --set-upstream origin  page_col
  # check 
  git branch -a # YAY!
# 3.b git push origin (nest times )
git push origin

Rename a (remote) branch

You need to 1. Publish an existing local branch on remote git push -u origin local_branch 2. So you delete old one and push up a new one from local repository

Merge a git branch into master

  1. List All Git Branches
  2. Switch to Master
  3. Merge Branch into Master
  4. Push Changes (push the local changes to the remote repository so everyone working on the project can fetch the latest version.)

Since merging is a type of commit, it also requires a commit message. There are two ways to specify the commit message:

# 1. List
git branch
# 2. Switch
git checkout master
# 3. The merge creates a merge commit, bringing together
    # lines of development while preserving the history of the source branch.
git merge -m "Prova di merge" page_col
# 4. Push the local changes to the remote repository
git push origin

See differences b/w branches

git diff master..page_col '***.qmd'Q

Git Rebase

  1. take commits from a separat branch and replay (shift the change down to the tip of master) them at the end of another brabch
  2. integrate recent commits without merging

“home base” branch to track changes on the main project

https://github.com/readme/guides/configure-git-environment

“Home base branch” is not a technical Git or GitHub term, but a phrase I use to describe a branch we’ll use to keep track of upstream repository changes, which will help us easily rebase our PRs in the future…. you’ll use it to rebase your development branches and create new working branches from it.

Basically in this example, my home base branch is named up. Your other development branches will be based on this up branch as well. In the following set of commands, Lulliter’s main branch is named master

Run these commands to setup your up branch to track changes in the upstream project repo:

# -- Terminal
git remote add upstream git@github.com:Lulliter/nerd_help.git 

git fetch upstream
# From github.com:Lulliter/nerd_help
#  * [new branch]      master     -> upstream/master
#  * [new branch]      new_sh     -> upstream/new_sh
#  * [new branch]      page_col   -> upstream/page_col

git checkout -b up upstream/master
# M       .gitignore
# M       docs/git/git_intro.html
# M       docs/search.json
# M       docs/sitemap.xml
# M       git/git_intro.qmd
# branch 'up' set up to track 'upstream/master'.
# Switched to a new branch 'up'

— WORKFLOW

The following workflow will help you make changes, submit a new PR, and update the same PR if necessary.

Whenever you want to create a new working branch, run the following commands:

# -- Terminal

git checkout up

git pull --rebase
# Then, create and switch to your working branch.
git checkout -b wkg_branch

# make and commit changes 
git add ...
git commit -m "your message"

#it’s time to push your changes to your remote fork. 
git push origin wkg_branch

# Opening the pull request

Once you’ve pushed your changes, you can use the GitHub WebUI to open the PR. Simply navigate to the main project page and GitHub will automatically suggest opening a PR from the changes that most recently got pushed to your fork.

# -- Terminal

#fetch any changes from upstream and then apply them to your up branch
git checkout up
# This means you will update your up branch to match with the latest changes in the repository where you are submitting your PR.
git pull --rebase
# switch back to your working branch
git checkout wkg_branch
# update your working branch (that contains your PR changes) to contain the latest changes from upstream (via the up branch) while preserving the changes you made on your working branch
git rebase up

# Now that your working branch is current, you can make your changes.
# To make changes to source code files you will edit the file(s), and run git add to stage # them for commit like you did before. 
git commit --amend
# Re-push your changes
git push -f origin wkg_branch 

Collaboration

  1. I create a test GH account lula-test (associated to l__a__a@icloud.com)

  2. I clone in /Users/testuser/GH_test/nerd_help this repository “nerd-help” I own as Lulliter and (from there (I indicated that lula-test is a collaborator)

# positioned in parent folder /Users/testuser/GH_test/
git clone https://github.com/Lulliter/nerd_help.git
cd nerd_help

Rules

  • THE MASTER BRANCH SHOULD ALWAYS BE DEPLOYABLE
    • you create new branches for new features and merge them into Master when they’re completed.

    • It’s also important when collaborating that your team picks features that don’t have overlapping code.

  1. Here I create a new branch colors_page and I go there
#  I create a new branch to add a color.qmd page and switch there 
git checkout -b colors_page 

# verify this with the command:
git branch
  • “checkout” is used to switch between branches. Adding the “-b” and a name at the end creates a new branch and then moves into that new branch for us.

In the branch, the collabrorator

  • makes some changes …
  • add + commit + push

3.b Wait

Even if I am in the collaborator GH, lookg like the remote is considered the owner GH

git remote -v         
# origin  https://github.com/Lulliter/nerd_help.git (fetch)
# origin  https://github.com/Lulliter/nerd_help.git (push)

so I must change te origin in the collaborator GH

git remote set-url origin  https://github.com/lula-test/nerd_help.git

Reference